package sketchpad;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import sketchpad.constant.Config;
import sketchpad.gui.component.*;
import sketchpad.gui.control.DrawControl;

public final class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane pane = new BorderPane();

        Scene scene = new Scene(pane, Config.WINDOW_LENGTH, Config.WINDOW_WIDTH);
        final ObservableList<String> stylesheets = scene.getStylesheets();
        stylesheets.addAll(Main.class.getResource("/css/component.css").toExternalForm(),
                            Main.class.getResource("/css/jfoenix-components.css").toExternalForm());

        //canvas
        MyCanvas myCanvas = new MyCanvas(primaryStage);//此时初始化drawControl
        pane.setCenter(myCanvas.getCanvas());
        DrawControl drawControl = myCanvas.getDrawControl();

        //添加选择按钮
        pane.setTop(MyHBox.constructHBox(drawControl,primaryStage,new FileExplorer()));
        //添加功能键
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setMinWidth(Config.WINDOW_LENGTH);
        anchorPane.setMinHeight(120);

        //添加画板
        ItemChipView itemChipView = new ItemChipView(drawControl);
        anchorPane.getChildren().add(itemChipView.constructItemChipView());
        drawControl.setChipView(itemChipView);
        Stage dialogStage = new Stage();
        PopupMenu popupMenu = new PopupMenu(drawControl, dialogStage);
        //添加功能按钮
        FuncNodeList funcNodeList = new FuncNodeList(drawControl,dialogStage,anchorPane);
        funcNodeList.reConstructNodesList();
        pane.setBottom(anchorPane);

        myCanvas.addCanvasListener(funcNodeList,popupMenu);

        primaryStage.setScene(scene);
        primaryStage.setTitle(Config.APP_NAME);
        primaryStage.setResizable(false);
        primaryStage.getIcons().add(new Image("/icon/画板.png"));
        primaryStage.show();
        primaryStage.setOnCloseRequest(event -> dialogStage.close());
    }

    public static void main(String[] args){
        launch(args);
    }
}
